package br3akr.DeadlyDinnerParty.Server.Actions;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

import br3akr.DeadlyDinnerParty.Server.Engine.Server;
import br3akr.DeadlyDinnerParty.Server.Engine.Session;
import br3akr.DeadlyDinnerParty.Server.Exceptions.DeadlyDinnerPartyException;
import br3akr.DeadlyDinnerParty.Server.World.GameCharacter;
import br3akr.DeadlyDinnerParty.Server.World.Handcuffs;
import br3akr.DeadlyDinnerParty.Server.World.Key;

public class CreateCharacterAction extends Action implements MultiStepAction {
	private static final int START  = 1;
	private static final int VERIFY_SEX = 2;
	private static final int CHAR_NAME = 3;
	private static final int VERIFY_NAME = 4;
	private static final int BUILD_CHARACTER = 5;
	private int curStep;
	private ActionResponse actionResponse;
	private boolean actionComplete;
	private String characterName;
	private GameCharacter.Gender gender;
	
	public static final Set<String> keywords;
	static
	{
		Set<String>tempKeywords = new HashSet<String>();
		keywords = Collections.unmodifiableSet(tempKeywords);
	}
    
    public CreateCharacterAction() {
        super("Login", false);
		curStep = 1;
		actionComplete = false;
    }
    
    public ActionResponse doAction(String action, Session session) throws DeadlyDinnerPartyException {
		if (session == null) {
			throw new DeadlyDinnerPartyException ("No session found.");
		}

        actionResponse = new ActionResponse();
        actionResponse.setActingCharacter(session.getCharacter());
		
		switch (curStep) {
			case START:
				if (!Server.testing || (Server.testing && !Server.isTestName(session.getPlayerName()))) {
					actionResponse.setActingCharacterDescription("Is your character (m)ale, or (f)emale?");
					curStep = VERIFY_SEX;
				} else {
					if (session.getPlayerName().equals("Test1")) {
						gender = GameCharacter.Gender.MALE;
					} else if (session.getPlayerName().equals("Test2")) {
						gender = GameCharacter.Gender.FEMALE;
					} else {
						gender = GameCharacter.Gender.MALE;
					}
					GameCharacter character = new GameCharacter(session.getPlayerName(), gender, "No descriptions yet.", null);
					character.setLocation(Server.world.getArea(3));

					if (session.getPlayerName().equals("Test1")) {
						character.addItem(Server.world.removeKey("Private Room One Key"));
						character.addItem(Server.world.removeKey("Private Room Two Key"));
					} else if (session.getPlayerName().equals("Test2")) {
						character.addItem(Server.world.removeKey("Private Room Three Key"));
						character.addItem(Server.world.removeKey("Private Room Four Key"));
						Handcuffs hc = new Handcuffs("Detective Handcuffs", "These well crafted handcuffs are reserved specifically for law enforcement.", null);
						Key hcKey = new Key("Detective Handcuffs Key", "These simple silver keys are designed for the detective's handcuffs.", null, null, null, false);
						hcKey.addHandcuffLock(hc);
						character.addItem(hc);
						character.addItem(hcKey);
					} else {
						Handcuffs hc = new Handcuffs("Merchant Handcuffs", "These well used handcuffs appear to still retain much of their old strength.", null);
						Key hcKey = new Key("Merchant Handcuffs Key", "This elaborate bronze key has a silver necklace and is used to unlock the Merchant Handcuffs.", null, null, null, false);
						hcKey.addHandcuffLock(hc);
						character.addItem(hc);
						character.addItem(hcKey);
						character.addItem(Server.world.removeKey("Private Room Five Key"));
					}
					
					Server.world.getArea(3).addCharacter(character);
					session.setCharacter(character);
					actionResponse.setActingCharacterDescription("Congratulations! You're a <insert class here>" + character.getName() + ". Good luck!");
					actionComplete = true;
				}
				break;
			case VERIFY_SEX:
				if (action.equalsIgnoreCase("M") || action.equalsIgnoreCase("Male")) {
					actionResponse.setActingCharacterDescription("So you would like your character to be Male?");
					gender = GameCharacter.Gender.MALE;
				} else if (action.equalsIgnoreCase("F") || action.equalsIgnoreCase("Female")) {
					actionResponse.setActingCharacterDescription("So you would like your character to be Female?");
					gender = GameCharacter.Gender.FEMALE;
				} else {
					actionResponse.setActingCharacterDescription("I'm sorry, you entered " + action + ", are you (M)ale, or (F)emale?");
				}
                curStep = CHAR_NAME;
				break;
			case CHAR_NAME:
				if (action.equalsIgnoreCase("Y") || action.equalsIgnoreCase("Yes")) {
                    actionResponse.setActingCharacterDescription("What would you like your character's name to be?");
                    curStep = VERIFY_NAME;
				} else {
                    actionResponse.setActingCharacterDescription("No? What will your gender be then?");
					curStep = VERIFY_SEX;
				}
				break;
			case VERIFY_NAME:
                actionResponse.setActingCharacterDescription("Your character name will be, " + action + ". Is that correct?");
                StringTokenizer st = new StringTokenizer(action);
                if (st.countTokens() > 1) {
                	actionResponse.setActingCharacterDescription("You can not have spaces in your name. What would you like your character's name to be?");
                	curStep = VERIFY_NAME;
                } else {
                	actionResponse.setActingCharacterDescription("Your character name will be, " + action + ". Is that correct?");
                	curStep = BUILD_CHARACTER;
                	characterName = action;
                }
				break;
			case BUILD_CHARACTER:
				if (action.equalsIgnoreCase("Y") || action.equalsIgnoreCase("Yes")) {
					GameCharacter character = new GameCharacter(characterName, gender, "No descriptions yet.", null);
					character.setLocation(Server.world.getArea(3));
					Server.world.getArea(3).addCharacter(character);
					session.setCharacter(character);
					actionResponse.setActingCharacterDescription("Congratulations! You're a <insert class here>" + character.getName() + ". Good luck!");
					actionComplete = true;
				} else {
					actionResponse.setActingCharacterDescription("What will your character name be then?");
					curStep = VERIFY_NAME;
				}
				break;
			default:
				break;
		}
        
        return actionResponse;
    }
	
    public ActionResponse getActionResponse() {
		return actionResponse;
	}

	public boolean isComplete() {
		return actionComplete;
	}
    
    public Set<String> getKeywords() {
        return keywords;
    }

	@Override
	public String getCurrentDescription() {
		return null;
	}
}